package com.mioto.ld.communication;

import com.mioto.ld.communication.mqtt.MqttHelper;
import com.mioto.ld.communication.mqtt.protocol.cache.ReportAttributeCache;
import com.mioto.ld.communication.mqtt.protocol.model.Message;
import com.mioto.ld.module.device.service.IDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author mioto-qinxj
 * @date 2024/6/20
 * @desc
 */
@Component
@Slf4j
public class DeviceOfflineProcess {
    @Resource
    private IDeviceService deviceService;
    @Resource
    private DeviceFunctionLogProcess deviceFunctionLogProcess;

    public void handler(Message message,boolean clearCache){
        log.info("=============device offline message process start=============");
        if (clearCache) {
            log.info("clear device all caches");
            MqttHelper.remove(message.getSn());
            ReportAttributeCache.remove(message.getSn());
        }
        log.info("update device last offline time");
        Date now = new Date();
        deviceService.updateLineDateBySn(message.getSn(),null,now);
        log.info("save device offline function log");
        deviceFunctionLogProcess.saveFunctionLog(message.getSn(),message.getMtype(),now);
        log.info("=============device offline message process end=============");
    }
}
